package com.zhima.controller.plain;

import com.zhima.annotation.MustLogin;
import com.zhima.dto.QuestionBankDTO;
import com.zhima.dto.ZhimaOjPageResult;
import com.zhima.dto.ZhimaOjResult;
import com.zhima.service.OjQuestionBankService;
import com.zhima.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.zhima.dto.ZhimaOjResult.success;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhima
 * @since 2023-08-25
 */
@RestController
@RequestMapping("/bank")
@MustLogin
public class OjQuestionBankController {
    @Autowired
    private OjQuestionBankService ojQuestionBankService;

    /**
     * 分页查询题库基本信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryQuestionBankBasicInfo(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize, @RequestParam("keyword") String keyword) {
        return ZhimaOjPageResult.success(ojQuestionBankService.pageQueryQuestionBankBasicInfo(UserHolder.get().getId(), pageNum, pageSize, keyword));
    }

    /**
     * 分页查询登录用户的题库列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/mine/{pageNum}/{pageSize}")
    public ZhimaOjPageResult pageQueryMineQuestionBank(@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return ZhimaOjPageResult.success(ojQuestionBankService.pageQueryMineQuestionBank(UserHolder.get().getId(), pageNum, pageSize));
    }

    /**
     * 返回这个题库的全部信息
     *
     * @param questionBankId
     * @return
     */
    @GetMapping("/{questionBankId}")
    public ZhimaOjResult queryQuestionBankDetailById(@PathVariable("questionBankId") Long questionBankId) {
        return success(ojQuestionBankService.queryQuestionBankDetailById(UserHolder.get().getId(), questionBankId));
    }

    /**
     * 编辑题单的时候查询详情
     *
     * @param questionBankId
     * @return
     */
    @GetMapping("/edit/{questionBankId}")
    public ZhimaOjResult queryQuestionBankEditById(@PathVariable("questionBankId") Long questionBankId) {
        return success(ojQuestionBankService.queryQuestionBankEditById(UserHolder.get().getId(), questionBankId));
    }

    /**
     * 登录用户创建一个新的题库
     * 返回创建的题库对象
     *
     * @return
     */
    @PostMapping
    public ZhimaOjResult createNewQuestionBank(@RequestBody QuestionBankDTO questionBankDTO) {
        return success(ojQuestionBankService.createNewQuestionBank(UserHolder.get().getId(), questionBankDTO));
    }


    /**
     * 删除自己的题单
     *
     * @param questionBankId
     * @return
     */
    @DeleteMapping("/{questionBankId}")
    public ZhimaOjResult deleteQuestionBankById(@PathVariable("questionBankId") Long questionBankId) {
        return success(ojQuestionBankService.deleteQuestionBankById(UserHolder.get().getId(), questionBankId));
    }

    /**
     * 更新题库的基本信息
     *
     * @param questionBankDTO
     * @return
     */
    @PutMapping
    public ZhimaOjResult updateQuestionBankBasicInfo(@RequestBody QuestionBankDTO questionBankDTO) {
        return success(ojQuestionBankService.updateQuestionBankBasicInfo(UserHolder.get().getId(), questionBankDTO));
    }
}
